package com.ysm.smfu.utils;

import android.text.TextUtils;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.ysm.smfu.bean.Dingdan;
import com.ysm.smfu.bean.User;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by Administrator on 2016/10/17.
 */
public class JsonUtil {
    /**
     * 格式:{"result":{"state":"1","code":"100001","url":null,"message":"注册成功"}}
     */
    public static String getCode(String response) {
        try {
            JSONObject jsonObject = new JSONObject(response);
            JSONObject result = jsonObject.optJSONObject("result");
            return result.optString("code");
        } catch (JSONException e) {

            return null;
        }
    }

    /**
     * 格式：{"result":{"state":"0","code":"100003","url":null,"message":"验证码失效"}}
     */
    public static String getMessage(String response) {
        if (TextUtils.isEmpty(response) || TextUtils.equals(response, "{}"))
            return "网络繁忙,请稍后重试";
        try {
            JSONObject jsonObject = new JSONObject(response);
            JSONObject result = jsonObject.optJSONObject("result");
            return result.optString("message");
        } catch (JSONException e) {

            return null;
        }
    }

    /**
     * 获取验证码
     *
     * @param response
     * @return
     */
    public static String getYzCode(String response) {
        try {
            JSONObject jsonObject = new JSONObject(response);
            return jsonObject.optString("code");
        } catch (JSONException e) {

            return null;
        }
    }

    /**
     * 获取返回状态
     *
     * @param response
     * @return
     */
    public static String getState(String response) {
        if (TextUtils.isEmpty(response) || TextUtils.equals(response, "{}"))
            return "-100";
        try {
            JSONObject jsonObject = new JSONObject(response);
            JSONObject result = jsonObject.optJSONObject("result");
            return result.optString("state");
        } catch (JSONException e) {
            return "-100";
        }
    }

    /**
     * 获取用户信息：商户信息和个人信息
     *
     * @param response
     * @return
     */
    public static User getUserInfo(String response) {
        User user = new Gson().fromJson(response, User.class);
        return user;
    }

    /**
     * 获取金额支付订单信息
     *
     * @param response
     * @return
     */
    public static Dingdan getDingdan(String response) {
        return new Gson().fromJson(response, Dingdan.class);
    }

    /**
     * 获取兑换券支付订单信息
     */
    public static Dingdan getJiFenDingdan(String response) {
        return new Gson().fromJson(response, Dingdan.class);
    }

    /**
     * 返回支付宝异步通知URl
     *
     * @param response
     * @return
     */
    public static String getAliPayUrl(String response) {
        String url = null;
        try {
            JSONObject json = new JSONObject(response);
            url = json.optString("url");
        } catch (JSONException e) {

        }
        return url;
    }

    /**
     * 获取微信支付信息
     *
     * @param response
     * @return
     */
    public static String[] getPrePayInfo(String response) {
        String[] payInfo = new String[2];
        try {
            JSONObject jsonObject = new JSONObject(response);
            payInfo[0] = jsonObject.optString("prePayInfo");
            JSONObject resParams = jsonObject.optJSONObject("resParams");
            payInfo[1] = resParams.optString("returnPoints");
        } catch (JSONException e) {

        }
        return payInfo;
    }

}
